/**
 * @file calendar.c
 *
 */

/*********************
*      INCLUDES
*********************/
#include "lv_watch.h"

#if USE_LV_WATCH_CALENDAR != 0

/*********************
*      DEFINES
*********************/

/**********************
*      TYPEDEFS
**********************/
typedef struct
{
    lv_watch_obj_ext_t old_ext;
    const char * month[12];
    const char * days[7];
} lv_calendar_obj_ext_t;

/**********************
*  STATIC PROTOTYPES
**********************/
static lv_obj_t * calendar_create(lv_obj_t * activity_obj);

/**********************
*  STATIC VARIABLES
**********************/

/**********************
*      MACROS
**********************/

/**********************
 *   GLOBAL FUNCTIONS
 ***********************/
void calendar_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED == e) {
        lv_watch_png_cache_all_free();
        lv_obj_t * obj = calendar_create(NULL);
        LV_ASSERT_MEM(obj);
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void calendar_prepare_destroy(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);

    lv_watch_png_cache_all_free();
}

static lv_obj_t * calendar_create(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CALENDAR;
        activity_ext.create = calendar_create;
        activity_ext.prepare_destory = calendar_prepare_destroy;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_calendar_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_calendar_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * calendar = lv_calendar_create(obj, NULL);
    lv_obj_set_size(calendar, lv_obj_get_width(obj), lv_obj_get_height(obj) - 4);
    lv_obj_align(calendar, obj, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_local_text_font(calendar,
                                     LV_CALENDAR_PART_BG,
                                     LV_STATE_DEFAULT,
                                     LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_obj_set_style_local_bg_color(calendar,
                                    LV_CALENDAR_PART_BG,
                                    LV_STATE_DEFAULT,
                                    LV_COLOR_BLACK);
    lv_obj_set_style_local_text_font(calendar,
                                     LV_CALENDAR_PART_HEADER,
                                     LV_STATE_DEFAULT,
                                     LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_obj_set_style_local_pad_top(calendar,
                                   LV_CALENDAR_PART_HEADER,
                                   LV_STATE_DEFAULT,
                                   10);
    lv_obj_set_style_local_pad_bottom(calendar,
                                      LV_CALENDAR_PART_HEADER,
                                      LV_STATE_DEFAULT,
                                      6);
    lv_obj_set_style_local_pad_left(calendar,
                                    LV_CALENDAR_PART_HEADER,
                                    LV_STATE_DEFAULT,
                                    10);
    lv_obj_set_style_local_pad_right(calendar,
                                     LV_CALENDAR_PART_HEADER,
                                     LV_STATE_DEFAULT,
                                     10);
    lv_obj_set_style_local_text_font(calendar,
                                     LV_CALENDAR_PART_DAY_NAMES,
                                     LV_STATE_DEFAULT,
                                     LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_obj_set_style_local_pad_top(calendar,
                                   LV_CALENDAR_PART_DAY_NAMES,
                                   LV_STATE_DEFAULT,
                                   4);
    lv_obj_set_style_local_pad_bottom(calendar,
                                      LV_CALENDAR_PART_DAY_NAMES,
                                      LV_STATE_DEFAULT,
                                      4);
    lv_obj_set_style_local_pad_left(calendar,
                                    LV_CALENDAR_PART_DAY_NAMES,
                                    LV_STATE_DEFAULT,
                                    10);
    lv_obj_set_style_local_pad_right(calendar,
                                     LV_CALENDAR_PART_DAY_NAMES,
                                     LV_STATE_DEFAULT,
                                     10);
    lv_obj_set_style_local_text_font(calendar,
                                     LV_CALENDAR_PART_DATE,
                                     LV_STATE_DEFAULT,
                                     LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_obj_set_style_local_pad_left(calendar,
                                    LV_CALENDAR_PART_DATE,
                                    LV_STATE_DEFAULT,
                                    10);
    lv_obj_set_style_local_pad_right(calendar,
                                     LV_CALENDAR_PART_DATE,
                                     LV_STATE_DEFAULT,
                                     10);
    lv_obj_set_style_local_bg_color(calendar,
                                    LV_CALENDAR_PART_DATE,
                                    LV_STATE_FOCUSED,
                                    LV_COLOR_MAKE(26, 173, 230));
    lv_obj_set_style_local_text_color(calendar,
                                      LV_CALENDAR_PART_DATE,
                                      LV_STATE_DISABLED,
                                      LV_COLOR_GRAY);

    ext->month[0] = lv_lang_get_text(WATCH_TEXT_ID_JANUARY);
    ext->month[1] = lv_lang_get_text(WATCH_TEXT_ID_FEBRUARY);
    ext->month[2] = lv_lang_get_text(WATCH_TEXT_ID_MARCH);
    ext->month[3] = lv_lang_get_text(WATCH_TEXT_ID_APRIL);
    ext->month[4] = lv_lang_get_text(WATCH_TEXT_ID_MAY);
    ext->month[5] = lv_lang_get_text(WATCH_TEXT_ID_JUNE);
    ext->month[6] = lv_lang_get_text(WATCH_TEXT_ID_JULY);
    ext->month[7] = lv_lang_get_text(WATCH_TEXT_ID_AUGUST);
    ext->month[8] = lv_lang_get_text(WATCH_TEXT_ID_SEPTEMBER);
    ext->month[9] = lv_lang_get_text(WATCH_TEXT_ID_OCTOBER);
    ext->month[10] = lv_lang_get_text(WATCH_TEXT_ID_NOVEMBER);
    ext->month[11] = lv_lang_get_text(WATCH_TEXT_ID_DECEMBER);
    lv_calendar_set_month_names(calendar, ext->month);

    ext->days[0] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_SUNDAY);
    ext->days[1] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_MONDAY);
    ext->days[2] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_TUESDAY);
    ext->days[3] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_WEDNESDAY);
    ext->days[4] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_THURSDAY);
    ext->days[5] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_FRIDAY);
    ext->days[6] = lv_lang_get_text(WATCH_TEXT_ID_CALENDAR_SATURDAY);
    lv_calendar_set_day_names(calendar, ext->days);

    hal_rtc_t cur_time;
    Hal_Rtc_Gettime(&cur_time);
    lv_calendar_date_t today;
    today.year = cur_time.tm_year;
    today.month = cur_time.tm_mon;
    today.day = cur_time.tm_mday;
    lv_calendar_set_today_date(calendar, &today);
    lv_calendar_set_showed_date(calendar, &today);

    lv_watch_obj_add_element(calendar);

    return obj;
}

#endif /*USE_LV_WATCH_CALENDAR*/
